package com.itboye.sunsun;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.net.Uri;
import android.os.Build.VERSION_CODES;
import android.os.Bundle;
import android.os.Environment;
import android.support.v4.app.Fragment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.itboye.sunsun.application.App;
import com.itboye.sunsun.constants.SPContants;
import com.itboye.sunsun.dialog.WarnDialog;
import com.itboye.sunsun.product.center.ProductCenterHomeActivity;
import com.itboye.sunsun.utils.DebugLog;
import com.itboye.sunsun.utils.SPUtils;
import com.itboye.sunsunhome.R;
import com.itboye.sunsunhome.www.aq.DeviceActivity;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.jeremyfeinstein.slidingmenu.lib.app.SlidingFragmentActivity;
import com.sunsun_sunsun_home.LeftFragment;

public class MainHomeEnglishActivity extends SlidingFragmentActivity implements
		OnClickListener {

	Button btn;
	private Fragment mContent;
	private TextView titleHome;
	TextView tvtel;

	private String resultData;
	LinearLayout layoutDevice,layoutProcunter;
	@Override
	@SuppressLint("InlinedApi")
	public void onCreate(Bundle savedInstanceState) {
		// requestWindowFeature(Window.FEATURE_NO_TITLE); // 无标题
		super.onCreate(savedInstanceState);
		App.ctx.addActivity(this);
		
		setContentView(R.layout.activity_english);
		layoutDevice=(LinearLayout) findViewById(R.id.layoutDevice);
		layoutDevice.setOnClickListener(this);
		layoutProcunter=(LinearLayout) findViewById(R.id.layoutProcunter);
		layoutProcunter.setOnClickListener(this);
		initSlidingMenu(savedInstanceState);
	}

	
	
	
	
	/**
	 * 初始化侧边栏
	 */
	private void initSlidingMenu(Bundle savedInstanceState) {

		tvtel = (TextView) findViewById(R.id.tvtel);
		btn = (Button) findViewById(R.id.btn);
		tvtel.setOnClickListener(this);
		btn.setOnClickListener(this);
		resultData = (String) SPUtils.get(App.ctx, null, SPContants.PHONE, "");

		tvtel.setText("TEL:" + resultData);// , roundview);
		// 如果保存的状态不为空则得到之前保存的Fragment，否则实例化MyFragment
		if (savedInstanceState != null) {
			mContent = getSupportFragmentManager().getFragment(
					savedInstanceState, "mContent");
		}

		// if (mContent == null) {
		// mContent = new TodayFragment();
		// }

		// 设置左侧滑动菜单
		setBehindContentView(R.layout.menu_frame_left);
		getSupportFragmentManager().beginTransaction()
				.replace(R.id.menu_frame, new LeftFragment()).commit();

		// 实例化滑动菜单对象
		SlidingMenu sm = getSlidingMenu();
		// 设置可以左右滑动的菜单
		sm.setMode(SlidingMenu.LEFT);
		// 设置滑动阴影的宽度
		sm.setShadowWidthRes(R.dimen.shadow_width);
		// 设置滑动菜单阴影的图像资源
		sm.setShadowDrawable(null);
		// 设置滑动菜单视图的宽度
		sm.setBehindOffsetRes(R.dimen.slidingmenu_offset);
		// 设置渐入渐出效果的值
		sm.setFadeDegree(0.35f);
		// 设置触摸屏幕的模式,这里设置为全屏
		sm.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
		// 设置下方视图的在滚动时的缩放比例
		sm.setBehindScrollScale(0.0f);

	}

	// @Override
	// protected void onSaveInstanceState(Bundle outState) {
	// super.onSaveInstanceState(outState);
	// getSupportFragmentManager().putFragment(outState, "mContent", mContent);
	// }

	/**
	 * 切换Fragment
	 * 
	 * @param fragment
	 */
	public void switchConent(Fragment fragment, String title) {
		mContent = fragment;
		getSupportFragmentManager().beginTransaction()
				.replace(R.id.content_frame, fragment).commit();
		getSlidingMenu().showContent();
		// titleHome.setText(title);
	}

	@Override
	public void onClick(View v) {
		Intent intent;
		switch (v.getId()) {
		case R.id.btn:
			toggle();
			break;
		case R.id.tvtel:
			 intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:"
					+ resultData));
			startActivity(intent);
			break;
		case R.id.layoutDevice:
//			DebugLog.v("wsh", "ssaq");
//			App.ctx.init();
//			intent=new Intent(this,DeviceActivity.class);
//			
//			startActivity(intent);
			
			
			
			final String uid = (String) SPUtils.get(MainHomeEnglishActivity.this, null,
					SPContants.USER_ID, "");

			boolean flag = (boolean) SPUtils.get(MainHomeEnglishActivity.this,
					"yubaomu_first_use", uid, true);
			if (flag == false) {
				App.ctx.init();
				startActivity(new Intent(MainHomeEnglishActivity.this, DeviceActivity.class));
			} else {

				new WarnDialog("TERMS AND  STATEMENT", getString(R.string.yubaomu_warning),
						new WarnDialog.OnDialogSelectListener() {

							@Override
							public void onDialoSelect(boolean isAgree,
									boolean noTip) {
								if (isAgree) {
									App.ctx.init();
									startActivity(new Intent(MainHomeEnglishActivity.this,
											DeviceActivity.class));

								} else {

								}
								if (noTip) {
									SPUtils.put(MainHomeEnglishActivity.this,
											"yubaomu_first_use", uid, false);
								}
							}
						}).show(getSupportFragmentManager(), null);

			}
			
			break;
		case R.id.layoutProcunter:
			 intent=new Intent(this,ProductCenterHomeActivity.class);
			startActivity(intent);
			break;
		default:
			break;
		}
	}

}
